#include <linux/init.h>
#include <linux/err.h>
#include <linux/platform_device.h>
#include <linux/jiffies.h>
#include <linux/slab.h>
#include <linux/time.h>
#include <linux/wait.h>
#include <linux/hrtimer.h>
#include <linux/math64.h>
#include <linux/module.h>
#include <sound/core.h>
#include <sound/control.h>
#include <sound/tlv.h>
#include <sound/pcm.h>
#include <sound/rawmidi.h>
#include <sound/info.h>
#include <sound/initval.h>
#include <sound/soc.h>



static int cpu_dai_probe(struct snd_soc_dai *dai)
{
	return 0;
}


static void cpu_dai_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
{

}


static int cpu_dai_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
{
    return 0;
}


static int cpu_dai_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
{
    return 0;
}



static int cpu_dai_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
{
    return 0;
}


static int cpu_dai_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
{
    return 0;
}


static int cpu_dai_set_clkdiv(struct snd_soc_dai *cpu_dai, int div_id, int div)
{
    return 0;
}


static int cpu_dai_set_tdm_slot(struct snd_soc_dai *cpu_dai, unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
{
    return 0;
}


static const struct snd_soc_dai_ops cpu_dai_ops = {
	.probe		    = cpu_dai_probe,
	.shutdown	    = cpu_dai_shutdown,
	.prepare	    = cpu_dai_prepare,
	.trigger	    = cpu_dai_trigger,
	.hw_params	    = cpu_dai_hw_params,
	.set_fmt	    = cpu_dai_set_fmt,
	.set_clkdiv	    = cpu_dai_set_clkdiv,
	.set_tdm_slot   = cpu_dai_set_tdm_slot,
};


static struct snd_soc_dai_driver cpu_dai = {
	.name = "tiny_cpu",
	.playback = {
		.channels_min = 2,
		.channels_max = 128,
		.rates = SNDRV_PCM_RATE_8000_96000,
		.formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE),
	},
	.capture = {
		.channels_min = 2,
		.channels_max = 128,
		.rates = SNDRV_PCM_RATE_8000_96000,
		.formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE),
	},
	.ops = &cpu_dai_ops,
};


static const struct snd_soc_component_driver cpu_component = {
	.name			= "tiny_cpu",
	.legacy_dai_naming	= 1,
};


static int snd_cpu_probe(struct platform_device *pdev)
{
    int ret;
	struct device *dev = &pdev->dev;

	pr_info("snd_cpu_probe\n");

	ret = snd_soc_register_component(dev, &cpu_component, &cpu_dai, 1);
    if(ret) {
        pr_info("snd_soc_register_component failed\n");
        return -1;
    }

	return 0;
}


static struct platform_driver snd_cpu_driver = {
	.probe		= snd_cpu_probe,
	.driver		= {
		.name	= "tiny_cpu_dev",
	},
};


struct platform_device *device;


static int __init snd_cpu_init(void)
{
	int ret  = platform_driver_register(&snd_cpu_driver);
	if (ret < 0) {
        pr_info("platform_driver_register failed\n");
        return -1;
    }

	device = platform_device_register_simple("tiny_cpu_dev", 0, NULL, 0);
    if(device == NULL) {
        pr_info("platform_device_register_simple failed\n");
        return -1;
    }

	return 0;
}


static void __exit snd_cpu_exit(void)
{
	platform_device_unregister(device);
	platform_driver_unregister(&snd_cpu_driver);
}


module_init(snd_cpu_init)
module_exit(snd_cpu_exit)


MODULE_DESCRIPTION("Tiny ALSA demo");
MODULE_LICENSE("GPL");
